// axios 的基本配置
import nProgress from 'nprogress'
import axios from 'axios'

const BASE_URL = process.env.NODE_ENV === 'development' ? '/api' : 'https://lianghj.top:3000'
export function myAxios(config) {
  // 实例化 axios
  const instance = axios.create({
    baseURL: BASE_URL,
    timeout: 5000
  })
  // 默认配置
  instance.defaults.headers.common['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8'
  // 请求开始前做的事
  instance.interceptors.request.use((req) => {
    nProgress.start()
    req.headers.Authorization = window.sessionStorage.getItem('token')
    return req
  })
  // 得到响应后做的事
  instance.interceptors.response.use((res) => {
    nProgress.done()
    return res
  })
  return instance(config)
}
